package com.zr.bookcorner.activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.CountDownTimer;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.lurencun.service.autoupdate.AppUpdate;
import com.lurencun.service.autoupdate.AppUpdateService;
import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.utils.BarUtil;
import com.zh.baselibrary.utils.ClickUtils;
import com.zh.baselibrary.utils.IntentUtils;

import com.zr.bookcorner.R;
import com.zr.bookcorner.SplashActivity;
import com.zr.bookcorner.service.MoniterAppAliveService;

import java.util.Calendar;

import butterknife.OnClick;

public class HomeActivity extends BaseNoIMActivity {

    private static final Long MILLISINFUTURE = (long) 5 * 60 * 1000;
    private static final Long COUNTDOWNINTERVAL = (long) 1 * 1000;

    private Integer count = 0;
    private Integer auxiliaryCount = 0; //辅助计数变量，用以使退出程序经过两道程序
    private Long start, end, firstStart, firstEnd; //firstStart, firstEnd为辅助计数变量，用以使退出程序经过两道程序
    private boolean imvHideClickAble=false;
    private CountDownTimer countTimer;
    private AppUpdate appUpdate;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {
        startService(new Intent(this,MoniterAppAliveService.class));
        appUpdate = AppUpdateService.getAppUpdate(this);
        countTimer = new CountDownTimer(MILLISINFUTURE, COUNTDOWNINTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                IntentUtils.instance().startActivity(HomeActivity.this, AdsActivity.class);
        }
        };
        sendShutDown();
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 8, AudioManager.FLAG_PLAY_SOUND);
    }

    private void sendShutDown() {
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY, 23);//小时
        instance.set(Calendar.MINUTE, 59);//分钟
        instance.set(Calendar.SECOND, 59);//秒
        AlarmManager alarmService = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent mIntent = new Intent();
        mIntent.setAction(BaseConstant.SHUT_DOWN);
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, mIntent, 0);//通过广播接收
        alarmService.set(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis() - BaseConstant.timeDifferent, broadcast);
    }

    @OnClick({R.id.tv_about, R.id.tv_name, R.id.imv_hide, R.id.ll_find, R.id.ll_manager, R.id.ll_return})
    public void onViewClicked(View view) {
        int id = view.getId();

        if (id == R.id.imv_hide) {
            threeClick();
            return;
        }
        if(id == R.id.tv_name){
            firstThreeClick();
            return;
        }
        if (ClickUtils.isFastClick()) {
            Class<? extends AppCompatActivity> to;
            switch (id) {
                case R.id.tv_about:
                    to = AboutActivity.class;
                    break;
                case R.id.ll_find:
                    to = QueryBookActivity.class;
                    break;
                case R.id.ll_manager:
                    to = ManagerActivity.class;
                    break;
                case R.id.ll_return:
                    to = CirCulateActivity.class;
                    break;
                default:
                    to = SplashActivity.class;
            }
            IntentUtils.instance().startActivity(this, to);
        }
    }

    private void firstThreeClick(){
        auxiliaryCount++;
        if (auxiliaryCount == 1) {
            firstStart = System.currentTimeMillis();
        }
        if (auxiliaryCount == 3) {
            firstEnd = System.currentTimeMillis();
        }
        if (auxiliaryCount >= 3) {
            if ((firstEnd - firstStart) < 700) {
                imvHideClickAble=true;
            }
            auxiliaryCount = 0;
        }
        if ((System.currentTimeMillis() - firstStart) > 1000) {
            auxiliaryCount = 0;
            firstStart = System.currentTimeMillis();
        }
    }

    private void threeClick() {
        if(!imvHideClickAble){
            return;
        }
        count++;
        if (count == 1) {
            start = System.currentTimeMillis();
        }
        if (count == 3) {
            end = System.currentTimeMillis();
        }
        if (count >= 3) {
            if ((end - start) < 700) {
                IntentUtils.instance().startActivity(this, AdminLoginActivity.class);
            }
            count = 0;
            imvHideClickAble=false;
        }
        if ((System.currentTimeMillis() - start) > 1000) {
            count = 0;
            start = System.currentTimeMillis();
            imvHideClickAble=false;
        }
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //获取触摸动作，如果ACTION_UP，计时开始。
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            countTimer.start();
            //否则其他动作计时取消
        } else {
            countTimer.cancel();
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onPause() {
        super.onPause();
        countTimer.cancel();
        appUpdate.callOnPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        countTimer.start();
        BarUtil.closeBar();
        appUpdate.callOnResume();
    }
}
